(function(KKBus){

    function RefuelingController() {
        var self = this;
        
     
        self.refueling = ko.observableArray();
        
           self.getRefueling= function(index) {
               
            self.refueling.removeAll();
            app.request++;
            
            var auto = app.Knockout.BusController.bus()[index];
            var autoID = auto.id();
            
            var liczba = autoID;    
            console.log(liczba);
            var url = 'rest/bus/'+liczba+'/refueling/';
                     
            app.request++;
                       
            
                               
                
        KKBus.Ajax.doGet(url, function(data, textStatus, jqXHR)
        {
      
        var jsData = JSON.parse(data);
        var i = jsData.length;
        
        
            while(i--){
                 self.refueling.push(new KKBus.RefuelingModel(jsData[i].id,jsData[i].liters,jsData[i].costs, jsData[i].busId)); 
                     }
                 app.request--;
                  
          if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " + RefuelingController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
            });
            
            };


      self.validation = ko.observable(false);
      self.errorMessage = ko.observable("Blad");
      self.success = ko.observable(false);
      
      self.addRefueling = function() {
                   
          var url = 'rest/refueling/';
                 
            var refueling = JSON.stringify({
                busId:$('#formBusIdR').val(),
                liters: $('#formLiters').val(),
                costs: $('#formCosts').val(),
                date: "2012-12-12"
             });
             
             if(KKBus.Validation.length($('#formBusIdR').val())
                 && KKBus.Validation.length($('#formLiters').val())
                 && KKBus.Validation.length($('#formCosts').val())){
             
                   if(KKBus.Validation.koszt($('#formCosts').val())){
                       
                       if(KKBus.Validation.licz($('#formLiters').val())){
                            KKBus.Ajax.doPost(url, refueling, function(result, textStatus, jqXHR) {
                                if (result == "ADDED") {
                                    
       
                                     self.success(true);
                                        $('#formBusIdR').val(' ');
                                        $('#formLiters').val(' ');
                                        $('#formCosts').val(' ');
                                     setTimeout(function() 
                                    {   
                                        
                                         
                                         self.success(false); 
                                                                 
                                    }, 3000);
                                                                         
                                      var temp = JSON.parse(refueling);

                                          var RefuelingAdd = new KKBus.RefuelingModel(temp.id,temp.liters,temp.costs,temp.busId);

                                          self.refueling.push(RefuelingAdd);      
                                }
                               });
                             }else{
                               
                               self.validation(true);
                               self.errorMessage("Błędna ilość paliwa!");
                               setTimeout(function() { self.validation(false); }, 3000);
                            
                           };
                            
                        }else{
                        
                        self.validation(true);
                        self.errorMessage("Błędny format danych dotyczących ceny!");
                        setTimeout(function() { self.validation(false); }, 3000);
                        
                    };
                    
                 }else{
                     
                     self.validation(true);
                     self.errorMessage("Puste pola!");
                     setTimeout(function() { self.validation(false); }, 3000);
                     
                 };
    };


        self.deleteRefueling = function(id) {
     
         var url = 'rest/refueling/'+id;
         KKBus.Ajax.doDelete(url, function(result, textStatus, jqXHR) {
              });   
              };    
              
              
       
    }
    KKBus. RefuelingController =  RefuelingController;
    
}(window.KKBus));

